package com.kelvin.oocl.filemanager.server.thread;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import com.kelvin.oocl.filemanager.constant.Constant;

public class UserThread extends Thread {
	private String baseDir;
	private Socket socket;
	
	public UserThread(Socket socket,String baseDir) {
		this.socket = socket;
		this.baseDir = baseDir;
	}

	@Override
	public void run() {
		BufferedOutputStream out = null;
		BufferedReader reader = null;
		BufferedInputStream in = null;
		try {
			in = new BufferedInputStream(socket.getInputStream());
			out = new BufferedOutputStream(socket.getOutputStream());
			reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			while(true){
				String line = reader.readLine().trim();
				System.out.println(line);
				switch (line) {
				case Constant.SCP:
					scp(out,reader,in);
					break;
				case Constant.LS:
					ls(out,reader,in);
					break;
				case Constant.RM:
					rm(out,reader,in);
					break;
				default:
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(null!=out){
					out.close();
				}
				if(null!=reader){
					reader.close();
				}
				if(null!=socket){
					socket.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void rm(BufferedOutputStream out, BufferedReader reader,BufferedInputStream in) throws IOException{
		out.write("comfirm to delete? Y or N".getBytes());
		out.flush();
		String line = reader.readLine();
		if("Y".equals(line.trim())){
			String fileName = reader.readLine();
			File file = new File(this.baseDir+fileName);
			if(file.exists()){
				file.delete();
			}
			out.write("delete success".getBytes());
			out.flush();
		}
	}

	private void ls(BufferedOutputStream out, BufferedReader reader,BufferedInputStream in) throws IOException{
		StringBuilder sb = new  StringBuilder();
		File file = new File(this.baseDir);
		if(file.exists()){
			File[] files = file.listFiles();
			if(files.length>0){
				for (int i=0;i<files.length;i++) {
					sb.append(files[i].getName()+",");
					if(i%10==0){
						sb.append("\r\n");
					}
				}
				sb.append("\r\n");
				out.write(sb.toString().getBytes());
			}else {
				out.write("no file to be found".getBytes());
			}
		}else {
			out.write("no file to be found".getBytes());
		}
		out.flush();
	}

	private void scp(BufferedOutputStream out, BufferedReader reader,BufferedInputStream in) throws IOException {
		out.write("scp\r\n".getBytes());
		out.flush();
		String fileName = reader.readLine();
		System.out.println(fileName);
		
		out.write("upload\r\n".getBytes());
		out.flush();
		BufferedOutputStream out1 = new BufferedOutputStream(new FileOutputStream(new File(this.baseDir+fileName)));
		byte[] buf = new byte[64];
		int n=-1;
		while(true){
			n = in.read(buf);
			if(buf[0]==Byte.MIN_VALUE){
				break;
			}
			out1.write(buf,0,n);
		}
		out1.flush();
		out1.close();
		out.write("upload success\r\n".getBytes());
		out.flush();
	}
	
}
